import 'package:flutter/material.dart';

class ExtendMenu extends StatefulWidget {
  final List<Widget> children;
  final int index;

  const ExtendMenu({
    Key key,
    this.children,
    this.index,
  }) : super(key: key);

  @override
  _ExtendMenuState createState() => _ExtendMenuState();
}

class _ExtendMenuState extends State<ExtendMenu> {
  Widget child;

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    print('didChangeDependencies');
  }

  @override
  void didUpdateWidget(ExtendMenu oldWidget) {
    super.didUpdateWidget(oldWidget);
    print('didUpdateWidget old index ${oldWidget.index}'
        ' new index ${widget.index}');
    if (oldWidget.index != widget.index) {
      var i = widget.index;
      child = i >= 0 ? widget.children[i] : null;
    }
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedSwitcher(
      duration: Duration(milliseconds: 150),
      transitionBuilder: (child, animation) => SizeTransition(
        sizeFactor: animation,
        child: child,
      ),
      child: child ?? Container(),
    );
  }
}
